const { request }  = require('../../request/index.js');
const ImageSource=  require('../../utils/image_path.js');
const crypto = require("../../utils/test");
const dataGet = require("../../request/dataGet.js");

const app=getApp()
Page({
    data: {
        NavbarHeight:app.globalData.navBarHeight + app.globalData.statusBarHeight,
        WindowWidth:app.globalData.windowWidth,
        navigationSetting:{
            title:'DIY专区',
            model:1,
        },
        diyType:1,
        cardId:0,
        cardsCategory:[],
        categoryName:[],
        category:0,
        colorId:0,
        imageNull:"",
        categoryIndex:[],
        designProduct:[],
        categoryAll:[],
        roleImageSrc:[],
        userData:[],
        designData:[],
        // designProduct:[{"拿铁咖啡":[{"id":3,"type":0,"name":"米色","category":"拿铁咖啡","imgSrc":null,"color":"#EAE2B7"}
        //         ,{"id":5,"type":0,"name":"蓝色","category":"拿铁咖啡","imgSrc":null,"color":"#709DEB"}
        //         ,{"id":7,"type":0,"name":"格子纹样","category":"拿铁咖啡","imgSrc":null,"color":"../../icons/pageDiy/" +
        //                 "icon_gridBackground.png"}]}
        //     ,{"生椰拿铁":[{"id":2,"type":0,"name":"米色","category":"生椰拿铁","imgSrc":null,"color":"#EAE2B7"}
        //             ,{"id":4,"type":0,"name":"褐色","category":"生椰拿铁","imgSrc":null,"color":"#33170A"}
        //             ,{"id":6,"type":0,"name":"格子纹样","category":"生椰拿铁","imgSrc":null,"color":"../../icons/pageDiy/icon_gridBackground.png"}]}],
        priceData:["29.90","59.90","19.90"],
        colorStyle:[],
        imageUrl:['../../icons/diy_icon/glass1.png',
                '../../icons/diy_icon/cup1.png',
                '../../icons/diy_icon/bottom1.png'],

    },
    onLoad(options){
        let type=options.type;
        this.styleSheetOnload(type)
        var title="";
        if(type==0) title="定制水杯"
        else if(type==1)title="定制保温杯"
        else if(type==2)title="定制杯垫"
        this.setData({
            navigationSetting:{
                title:title,
                model:1,
            },
            diyType:type,
            colorStyle:this.data.designProduct[this.data.category],
        })
    },
    styleSheetOnload(type){
        let params={
            url:'/stylesheet',
            method: 'GET',
            data: {
                type:type,
            },
        }
        request(params).then(res=>{
            console.log(this.data.diyType)
            var that=this;
            console.log(JSON.parse(crypto.AES_DE(res.data)))
            var values=Object.values(JSON.parse(crypto.AES_DE(res.data)));
            var categoryAll=Object.keys(JSON.parse(crypto.AES_DE(res.data)));
            var fileList=[];
            var index;
            if(this.data.diyType==0)index=3;
            else if(this.data.diyType==1)index=4;
            else index=5;
            console.log(index)
            for(let i=0;i<6;i++){
                for(let j=0;j<values[i].length;j++){
                    fileList.push(ImageSource.Base+ImageSource.page[index]+values[i][j].imgSrc)
                }
            }
            var title=["cup","mug","pad"]
            fileList.push(ImageSource.Base+ImageSource.page[index]+title[this.data.diyType]+"_null.png")
            new Promise((resolve)=>{
                wx.cloud.getTempFileURL({
                    fileList:fileList,
                    success:(r)=>{
                        resolve(r)
                    }
                })
            }).then((r)=>{
                console.log("------")
                console.log(r)
                var count=0;
                for(var i=0;i<6;i++){
                    for(var j=0;j<values[i].length;j++){
                        //console.log(r.fileList[count].tempFilePath)
                        values[i][j].imgSrc = r.fileList[count++].tempFileURL;
                    }
                }
                that.setData({
                    imageNull:r.fileList[count].tempFileURL,
                    categoryAll:categoryAll,
                    designProduct:values,
                })
                console.log("--------++++")
                console.log(that.data.designProduct)
                })
            //new Promise(resolve =>
        })
    },
    onShow(){
        const index=this.data.categoryAll.indexOf(this.data.categoryName);
        this.setData({
            categoryIndex:index,
            colorStyle:this.data.designProduct[index],
        })
        console.log("-------------")
        console.log(this.data.cardId);
        const data=wx.getStorageSync("userCards");
        console.log(data.data.flat(1))
        const obj=data.data.flat(1).find(obj=>{
            return obj.id==this.data.cardId;
        })
        console.log(obj)
        this.setData({
            designData:obj
        })
        if(obj!=null)
            var src=obj.imageSrc.split("/").pop();
        src=ImageSource.Base+"/icon/pageDiy/role/"+src
        var that=this;
        wx.cloud.getTempFileURL({
            fileList:[src],
            success:res=>{
                that.setData({
                    roleImageSrc:res.fileList[0].tempFileURL,
                })
            }
        })
    },
    returnPage(){
        wx.navigateBack({
            delta: 1
        });
    },
    skipChooseItemTap(){
        wx.navigateTo({
            url:'../diyChoose/index?title='+this.data.navigationSetting.title+'&cardId='+this.data.cardId+'&category='+this.data.category,
        })
    },
    colorChangeItemTap(e){
        const index=e.currentTarget.dataset.index
        console.log(index)
        this.setData({
            colorId:index
        })
    },
    payItemTap(){
        this.onloadUserMessage();
        setTimeout(() =>{
            console.log(this.data.userData)
        },400)

    },
    async onloadUserMessage(){
        if(app.globalData.userDataChangeFlag==1){
            await dataGet.getUserData()
            app.globalData.userDataChangeFlag=0
        }
        var userData=wx.getStorageSync("userData");
        if(!userData.data.address){
            var obj=JSON.stringify(userData.data);
            wx.navigateTo({
                url:'../setting/index?data='+encodeURIComponent(obj),
            })
            app.globalData.userDataChangeFlag=1;
        }else{
            var list=[];
            list.push(this.data.diyType)
            var str=this.data.designProduct[this.data.categoryIndex][this.data.colorId].imgSrc.split('/');
            list.push(str[str.length-1])
            str=this.data.roleImageSrc.split('/');
            list.push(str[str.length-1])
            list.push(this.data.colorStyle[this.data.colorId].name)
            list.push(this.data.designData.category)
            list.push(this.data.designData.cardOrder)
            list.push(app.globalData.openId)
            list.push(app.globalData.nowTime)
            var message=list.join('@')
            console.log(message)
            let params={
                url:'/makeOrder',
                method: 'POST',
                header: {
                    "Content-Type": "application/x-www-form-urlencoded" //POST方式是这个
                },
                data: {
                    message:crypto.AES_EN(message),
                },
            }
            request(params)
            app.globalData.userOrdersChangeFlag=1;
            wx.showModal({
                title:"成功购买，您可以去我的-订单进行查看",
                duration:1500
            })
        }

    }
})